define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap"
],function($,IO,bootbox,bootstrap){
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '编号',
                dataIndex: 'message_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].message_id+"</span>";
                }
            },
            {
                title: '来源',
                dataIndex: 'message_from',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].message_from+"</span>";
                }
            },
            {
                title: '接受者',
                dataIndex: 'message_to',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].message_to+"</span>";
                }
            },
            {
                title: '消息类型',
                dataIndex: 'message_type',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var message_type = data[datarow].message_type;
                    if(message_type == "0"){
                        return "文字"
                    }
                    if(message_type == "1"){
                        return "图片"
                    }
                    if(message_type == "2") {
                        return "语音"
                    }
                }
            },
            {
                title: '消息内容',
                dataIndex: 'message_body',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var message_type = data[datarow].message_type;
                    var back_data = data[datarow].message_body;
                    //if(message_type == "2"){
                    //    return "<audio src='"+back_data+"'controls='controls'/>"
                    //}
                    if(message_type == "1"){
                        return "<img style='width: 50px;height: 50px' src='"+back_data+"' alt='消息图片'/>"
                    }
                    if(message_type == "0" || message_type == "2"){
                        var comment_body;
                        if(back_data.length>10){
                            comment_body = back_data.substring(0,10) + "......";

                        }else{
                            comment_body = back_data;
                        }
                        return "<span>"+comment_body +"</span>";
                    }
                    return "<span>"+back_data+"</span>";
                }
            },
            {
                title: '消息时间',
                dataIndex: 'gender',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var date = new Date(data[datarow].timestamp);
                    create_year = date.getFullYear();
                    create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
                    create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
                    create_hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
                    create_min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
                    create_sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                    return "<span>"+create_year+'-'+create_month+'-'+create_day+' '+create_hours+':'+create_min+':'+create_sec+ ' '+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' class='consultationClassification_detail' style='color: green;' message_id='"+data[datarow].message_id+"'>展示详情</a></span>";
                }
            },

        ];


        //数组初始化
        function dataInit() {
            grid = new Grid("/ct/act/chatManagement_list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            return "";
        }
        dataInit();

    });

    $("#export").on('click',function(){
        window.location.href = "http://zjprod.snsunion.cn:8081/CorporateTraining/api/v1/messages/export";
    })
    //隐藏后操作
    $("#myModal").on('hidden.bs.modal',function(){
        $("#message_body").text('');
    });
    /**---------------------------------获取展示详情--------------------------------*/
    $(document).on("click",".consultationClassification_detail",function(){
        var message_id=$(this).attr("message_id");
        IO.get("/ct/act/chat_detail",{message_id:message_id},function(d){
            var data = d.data;
            $("#message_id").val(data.message_id);
            //时间
            var date = new Date(data.timestamp);
            create_year = date.getFullYear();
            create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
            create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
            create_hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
            create_min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
            create_sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            $("#timestamp").val(create_year+'-'+create_month+'-'+create_day+' '+create_hours+':'+create_min+':'+create_sec+ ' ');
            //
            $("#message_from").val(data.message_from);
            $("#message_to").val(data.message_to);
            //类型   内容
            var message_type = data.message_type;
            var message_body = data.message_body;
            if(message_type == "0"){
                $("#message_type").val("文字");
                $("#message_body").text(message_body);
            }
            if(message_type == "1"){
                $("#message_type").val("图片");
                $("#message_body").append('<img src="'+message_body+'" style="height: 200px;width: 200px" alt="消息图片"/>');
            }
            if(message_type == "2"){
                $("#message_type").val("语音");
                $("#message_body").append('<audio src="'+message_body+'" controls="controls" />');
            }
        });
        $("#myModal").modal('show');

    });
})
